package com.jia.rpc;

import com.jia.rpc.config.RpcConfig;
import com.jia.rpc.constant.RpcConstant;
import com.jia.rpc.utils.ConfigUtils;
import lombok.extern.slf4j.Slf4j;

import javax.xml.crypto.dsig.spec.ExcC14NParameterSpec;

/**
 * 存放全局变量
 */
@Slf4j
public class RpcApplication {
    private static volatile RpcConfig rpcConfig;


    public static void init(RpcConfig newRpcConfig) {
        rpcConfig = newRpcConfig;
        log.info("rpc init, config = {}", newRpcConfig.toString());
    }


    public static void init(){
        RpcConfig newRpcConfig;
        try {
            newRpcConfig = ConfigUtils.loadConfig(RpcConfig.class, RpcConstant.DEFAULT_CONFIG_PRERIX);
        }catch (Exception e) {
            //配置加载失败，使用默认值
            newRpcConfig = new RpcConfig();
        }
        init(newRpcConfig);
    }

    public static RpcConfig getRpcConfig(){
        if(rpcConfig == null) {
            synchronized (RpcApplication.class) {
                if(rpcConfig == null) {
                    init();
                }
            }
        }
        return rpcConfig;
    }
}
